package com.baitengsoft.patentapimonitor.api.controller;

import com.baitengsoft.patentapimonitor.api.entity.WebUserinfoEntity;
import com.baitengsoft.patentapimonitor.api.model.CfgDictionaryInfo;
import com.baitengsoft.patentapimonitor.api.model.WebAreaInfo;
import com.baitengsoft.patentapimonitor.api.model.param.WebPatentApplySearchParam;
import com.baitengsoft.patentapimonitor.api.service.CfgDictionaryService;
import com.baitengsoft.patentapimonitor.api.service.WebPatentApplyService;
import com.baitengsoft.patentapimonitor.api.service.WebPlatformareaService;
import com.baitengsoft.patentapimonitor.common.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;

/**
 * 专利申请查询
 * @author Chen 2018/1/8
 */
@RestController
@RequestMapping("/api/v1/web/analysis/patentApply")
@Api(value = "前台-统计分析-专利申请查询",description = "专利申请查询")
public class WebPatentApplyController extends AbstractController {

    @Autowired
    private WebPatentApplyService webPatentApplyService;
    @Autowired
    private WebPlatformareaService webPlatformareaService;
    @Autowired
    private CfgDictionaryService cfgDictionaryService;

    /**
     * 获取筛选参数
     */
    @ApiOperation(value ="获取专利申请查询筛选,参数平台下区域(webAreaInfos),获取申请人类型(applicants),专利类型(patTypes)")
    @GetMapping("/param")
    @ResponseBody
    public Res getPatentApplyParam(){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        //获取平台下区域ID和名称
        List<WebAreaInfo> webAreaInfos = webPlatformareaService.getNameAndIdByPlatformId(userInfo.getPlatformid());
        WebAreaInfo info  =new WebAreaInfo();//未分类
        info.setAreaId(0);
        info.setAreaName("未分类");
        webAreaInfos.add(info);
        //获取专利类型和value
        List<CfgDictionaryInfo> patTypes = cfgDictionaryService.getNameAndValueByCode("1003");
        //获取申请人类型
        List<CfgDictionaryInfo> applicants = cfgDictionaryService.getNameAndValueByCode("1004");
        return Res.ok().put("webAreaInfos",webAreaInfos).put("patTypes",patTypes).put("applicants",applicants);
    }

    /**
     *获取专利申请查询列表
     */
    @ApiOperation(value ="获取专利申请查询列表")
    @GetMapping("/applicantAnalysis")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patType", value = "专利类型 字符串\"1,2,3\"", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applicants", value = "申请人类型", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patNum", value = "申请号", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patName", value = "专利名称", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "address", value = "地址", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "applyPerson", value = "专利申请人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "agencyName", value = "代理人", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "startDate", value = "开始时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "endDate", value = "结束时间", required = false),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNumber", value = "页号", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页多少条", required = true)
    })
    public Res getWebPatentApplyList(@ApiIgnore WebPatentApplySearchParam param){
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
       return  webPatentApplyService.getWebPatentApplyList(param,userInfo.getPlatformid());
    }

    @ApiOperation(value ="归类")
    @GetMapping("/classify")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "patId", value = "专利ID", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "areaId", value = "区域ID", required = true)
    })
    public Res classifyPat(String patId,Integer areaId){
         if(patId == null || areaId ==null){
             return Res.error("参数有误");
         }
         if (areaId <0){
             return Res.error("区域参数有误");
         }
        WebUserinfoEntity userInfo = getUserInfo();
        if (userInfo ==null || userInfo.getPlatformid()==null){
            return Res.error("用户信息获取失败");
        }
        if(patId.trim().equals("")){
            return Res.error("参数有误");
        }
        String[] split = patId.split(",");
        List<String> list = Arrays.asList(split);
        return webPatentApplyService.classifyPat(list,areaId);
    }
}
